﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Colorful
{
    /// <summary>
    /// 安全类Helper
    /// </summary>
    public static class SecurityHelper
    {
        /// <summary>
        /// 根据传入的明文密码获取加密后的密码
        /// </summary>
        /// <param name="password">明文密码</param>
        /// <returns></returns>
        public static string GetPassword(string password)
        {
            password = AESHelper.Encrypt(password);
            
            var sha256 = System.Security.Cryptography.SHA256.Create();
            var data = sha256.ComputeHash(System.Text.Encoding.ASCII.GetBytes(password));
            var result = new StringBuilder();
            for (var i = 0; i < data.Length; i++)
                result.Append(data[i].ToString("x2"));
            return result.ToString();
        }
        /// <summary>
        /// 获取签名字符串
        /// </summary>
        /// <param name="key">用于生成签名的Key</param>
        /// <param name="prefix">字符串前辍</param>
        /// <param name="nonce">唯一字符串：db.GetUniqueId()</param>
        /// <param name="timestamp">时间戳：DateTime.Now.Ticks.ToString()</param>
        /// <returns></returns>
        public static string GetSignature(string key, string prefix, string nonce, string timestamp)
        {
            var str = new StringBuilder(prefix);
            str.Append("&nonce=");
            str.Append(nonce);
            str.Append("&timestamp=");
            str.Append(timestamp);
            return str.ToString().ToLower().ToSHA1(key);
        }
    }
}
